﻿using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Service.Material;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Dispatcher;
using Lifter.Device.Unit;
using Lifter.Global;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels;

namespace Lifter.Gui.Monitor.Popups
{
    public partial class MaterialEdit : UIEditForm
    {
        private LifterSEM lftSem = null;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        public MaterialEdit()
        {
            InitializeComponent();
            if (this.lftSem == null)
            {
                this.lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            }
            //加载物料当前位置
            this.LoadcbMaterialLocalData();
            //加载物料类型
            this.LoadcbMaterialTypeData();
            //加载物料状态
            this.LoadcbStatusData();
            //加载物料ID数据
            this.LoadcbMaterialIDData();
            //加载指令ID数据
            this.LoadcbCommandIDData();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cbMaterialID, "请输入物料ID");
        }
        public async void UpdateManualMaterial()
        {
            try
            {
                //判断输入的信息是否能创建指令
                string materialID = this.cbMaterialID.Text;
                string materialLocal = this.cbMaterialLocal.Text;
                string commandID = this.cbCommandID.Text;
                string materialTypeText = this.cbMaterialType.Text;
                string materialStatusText = this.cbStatus.Text;

                CarrierState materialStatus = CarrierState.StringToValue(materialStatusText);
                CarrierType materialType = CarrierType.StringToValue(materialTypeText);
                //获取物料对象是否存在
                Carrier carrier = this.lftSem.GetCarrierByCarrierID(materialID);

                if (carrier != null)
                {
                    carrier.CarrierLoc = materialLocal;
                    carrier.CommandID = commandID;
                    carrier.CarrierState = materialStatus;
                    carrier.CarrierType = materialType;

                    carrier.Update();

                    this.ShowSuccessDialog(string.Format("手动更新物料[{0}]成功",materialID));
                    LogHelper.loggerInfo(string.Format("手动更新物料[{0}]成功", materialID));
                    LogHelper.operationLogger(string.Format("用户[{0}]更新物料成功，物料ID为:[{1}]", this.lftSem.UserName, materialID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]更新物料成功，物料ID为:[{1}]", this.lftSem.UserName, materialID));
                    MaterialInfoControl.materialInfoControl.UpdateDelegate();
                }
                else
                {
                    this.ShowWarningDialog("手动更新物料失败,物料信息不存在");
                    LogHelper.loggerInfo("手动更新物料失败，物料信息不存在");
                    LogHelper.operationLogger(string.Format("用户[{0}]更新物料失败，物料ID为:[{1}]，失败原因:物料信息不存在", this.lftSem.UserName, materialID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]更新物料失败，物料ID为:[{1}]，失败原因:物料信息不存在", this.lftSem.UserName, materialID));
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("手动更新物料报错",ex);
            }
            
        }
        /// <summary>
        /// 加载物料当前位置控件数据
        /// </summary>
        private void LoadcbMaterialLocalData()
        {
            List<string> LocationIDList = new List<string>();
            //加载Port口ID
            foreach (LifterPort lifterPort in this.lftSem.LifterPorts.Values)
            {
                LocationIDList.Add(lifterPort.HostID);
            }
            //加载CV ID
            foreach (ConveyorBuffer conveyorBuffer in this.lftSem.ConveyorBuffers.Values)
            {
                LocationIDList.Add(conveyorBuffer.HostID);
            }
            //加载轿厢CV ID
            foreach (Vehicle vehicle in this.lftSem.Vehicles.Values)
            {
                LocationIDList.Add(vehicle.HostID);
            }
            this.cbMaterialLocal.Items.AddRange(LocationIDList.ToArray());
        }
        /// <summary>
        /// 加载物料类型控件数据
        /// </summary>
        private void LoadcbMaterialTypeData()
        {
            List<string> materialTypeList = new List<string>();

            materialTypeList.Add(CarrierType.displayStrings[2]);
            materialTypeList.Add(CarrierType.displayStrings[5]);
            materialTypeList.Add(CarrierType.displayStrings[6]);
            materialTypeList.Add(CarrierType.displayStrings[7]);

            this.cbMaterialType.Items.AddRange(materialTypeList.ToArray());
        }

        /// <summary>
        /// 加载物料状态控件数据
        /// </summary>
        private void LoadcbStatusData()
        {
            List<string> materialStatus = new List<string>();

            materialStatus.Add(CarrierState.displayStrings[0]);
            materialStatus.Add(CarrierState.displayStrings[1]);
            materialStatus.Add(CarrierState.displayStrings[2]);
            materialStatus.Add(CarrierState.displayStrings[5]);
            materialStatus.Add(CarrierState.displayStrings[6]);

            this.cbStatus.Items.AddRange(materialStatus.ToArray());
        }
        /// <summary>
        /// 加载物料ID控件数据
        /// </summary>
        private void LoadcbMaterialIDData()
        {
            this.cbMaterialID.Items.Clear();
            List<string> carriers = Naming.GetCarrierManager().GetCarriersId();
            this.cbMaterialID.Items.AddRange(carriers.ToArray());
        }
        /// <summary>
        /// 加载物料指令ID控件数据
        /// </summary>
        private void LoadcbCommandIDData()
        {
            List<TransferWorkItem> transferWorkItems = this.lftSem.LDispatcher.workList;
            List<string> commandIDs = new List<string>();
            foreach (TransferWorkItem transferWork in transferWorkItems)
            {
                commandIDs.Add(transferWork.CommandID);
            }
            this.cbCommandID.Items.AddRange(commandIDs.ToArray());
        }

        private void cbMaterialID_TextChanged(object sender, EventArgs e)
        {
            string materialID = this.cbMaterialID.Text;
            if (!string.IsNullOrEmpty(materialID))
            {
                Carrier carrier = this.lftSem.GetCarrierByCarrierID(materialID);
                this.cbMaterialLocal.Text = carrier.CarrierLoc;
                this.cbCommandID.Text = carrier.CommandID;
                this.cbMaterialType.Text = CarrierType.ValueToString(carrier.CarrierType);
                this.cbStatus.Text = CarrierState.ValueToString(carrier.CarrierState);
            }
        }
    }
}